package com.googlecode.animeclassifier.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import com.googlecode.animeclassifier.Anime;
import com.googlecode.animeclassifier.AnimeManager;
import com.googlecode.animeclassifier.attribute.Attributes;

public class BrowsePanel extends JPanel {
	JPanel parentPanel;
	AnimeManager animeManager;
	JTable table;

	/**
	 * @param parentPanel
	 * @param animeManager
	 */
	public BrowsePanel(JPanel parentPanel, AnimeManager animeManager) {
		super(new BorderLayout());
		this.parentPanel = parentPanel;
		this.animeManager = animeManager;
		this.table = setupTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		this.add(scrollPane, BorderLayout.CENTER);
		
		// not implement yet
		// this.add(setupSouthPane(), BorderLayout.SOUTH);
	}

	JTable setupTable() {
		table = new JTable(new MyTableModel(animeManager.getAnimes()));
		table.setPreferredScrollableViewportSize(new Dimension(1200, 800));
		table.setFillsViewportHeight(true);
		for (int i = 0; i < ColumnSettings.getCols().size(); i++) {
			table.getColumnModel().getColumn(i).setPreferredWidth(ColumnSettings.getCols().get(i).getPreferredWidth());
		}
		table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					row = table.convertRowIndexToModel(row);
					System.out.println(row);
					animeManager.playAnime(row);
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});

		return table;
	}
	
    JPanel setupSouthPane() {
   	 JPanel southPanel = new JPanel();
        southPanel.add(new JLabel("Filter"));
        JTextField field = new JTextField("", 20);
        southPanel.add(field);
        field.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {}

			@Override
			public void keyReleased(KeyEvent e) {
				System.out.println(((JTextField) e.getComponent()).getText());
			}

			@Override
			public void keyTyped(KeyEvent e) {}
        });
        
        return southPanel;
   }

	class MyTableModel extends AbstractTableModel {
		List<Anime> animes;
		String nameFilter;

		public MyTableModel(List<Anime> animes) {
			this.animes = animes;
		}

		public int getColumnCount() {
			return ColumnSettings.getCols().size();
		}

		public int getRowCount() {
			return animes.size();
		}

		public String getColumnName(int col) {
			return ColumnSettings.getCols().get(col).getTitle();
		}

		public Object getValueAt(int row, int col) {
			switch (col) {
			case 0:
				return row + 1;
			case 1:
				return (animes.get(row).getAttrs().get(Attributes.VOLUME) != null) ? animes.get(row).getAttrs().get(
						Attributes.VOLUME).getString() : "";
			case 2:
				return animes.get(row);
			case 3:
				return (animes.get(row).getAttrs().get(Attributes.YEAR) != null) ? animes.get(row).getAttrs().get(
						Attributes.YEAR).getString() : "";
			case 4:
				return (animes.get(row).getAttrs().get(Attributes.SEASON) != null) ? animes.get(row).getAttrs().get(
						Attributes.SEASON).getString() : "";
			case 5:
				return (animes.get(row).getAttrs().get(Attributes.NAME) != null) ? animes.get(row).getAttrs().get(
						Attributes.NAME).getString() : "";
			}
			return "";
		}

		/*
		 * JTable uses this method to determine the default renderer/ editor for
		 * each cell. If we didn't implement this method, then the last column
		 * would contain text ("true"/"false"), rather than a check box.
		 */
		public Class getColumnClass(int c) {
			return getValueAt(0, c).getClass();
		}

		public boolean isCellEditable(int row, int col) {
			return false;
		}

		/*
		 * Don't need to implement this method unless your table's data can
		 * change.
		 */
		public void setValueAt(Object value, int row, int col) {
			// if (DEBUG) {
			// System.out.println("Setting value at " + row + "," + col
			// + " to " + value
			// + " (an instance of "
			// + value.getClass() + ")");
			// }
			//
			// data[row][col] = value;
			// fireTableCellUpdated(row, col);
			//
			// if (DEBUG) {
			// System.out.println("New value of data:");
			// printDebugData();
			// }
		}
		
		public void setNameFilter(String filter) {
			this.nameFilter = filter;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#repaint()
	 */
	@Override
	public void repaint() {
//		setupTable();
		super.repaint();
	}

}
